package com.pactera.asmp.server.restfullapi;

import com.alibaba.fastjson.JSON;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.entity.MasterCanDto;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.service.ICanInfoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: tlsserver
 * @Package com.pactera.asmp.server.restfullapi
 * @ClassName: RestfulCanInfoApi
 * @Description: can 信息
 * @Date 2020/06/01 11:05
 * @Author lvzhiqiang
 * @Version 1.0
 */
@RestController
@RequestMapping("/terminal/api/v1")
@Slf4j
public class RestfulCanInfoApi {
    @Autowired
    private ICanInfoService canInfoService;

    /**
     * @api {POST} /terminal/api/v1/can/push pushList
     * @apiVersion 1.0.0
     * @apiGroup RestfulCanInfoApi
     * @apiName pushList
     * @apiDescription 上位机-can设备状态等信息同步
     * @apiParam (请求体) {String} cuid 上位机唯一标识
     * @apiParam (请求体) {Array} canList 当前上位机下的 can 设备列表
     * @apiParam (请求体) {String} canList.duid 设备唯一标识
     * @apiParam (请求体) {Number} canList.deviceIndex can 设备索引
     * @apiParam (请求体) {Number} canList.channelIndex can 通道索引
     * @apiParam (请求体) {Number} canList.state 状态，1-连接，2-断线
     * @apiParam (请求体) {String} canList.baudRate 波特率
     * @apiParam (请求体) {Number} canList.passage 通道
     * @apiParamExample 请求体示例
     * {"cuid":"AzHrQkZ","canList":[{"baudRate":"c4","duid":"AQ","passage":5673,"state":1,"deviceIndex":3413,"channelIndex":9071}]}
     * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} Request-Example:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":1929,"data":{},"message":"5TTUVC"}
     */
    @PostMapping("/can/push")
    public ResponseResult pushList(@RequestBody MasterCanDto masterCanDto) {
        log.info("/terminal/api/v1/can/push masterCanDto: {}", JSON.toJSONString(masterCanDto));
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(canInfoService.push(masterCanDto));
        return responseResult;
    }

    /**
     * @api {POST} /terminal/api/v1/can/heartBeat heartBeat
     * @apiVersion 1.0.0
     * @apiGroup RestfulCanInfoApi
     * @apiName heartBeat
     * @apiDescription 上位机-can设备状态心跳
     * @apiParam (请求体) {String} cuid 上位机唯一标识
     * @apiParam (请求体) {Array} canList 当前上位机下的 can 设备列表
     * @apiParam (请求体) {String} canList.duid 设备唯一标识
     * @apiParam (请求体) {Number} canList.deviceIndex can 设备索引
     * @apiParam (请求体) {Number} canList.channelIndex can 通道索引
     * @apiParam (请求体) {Number} canList.state 状态，1-连接，2-断线
     * @apiParamExample 请求体示例
     * {"cuid":"s","canList":[{"duid":"8VBN3BlQ","state":1,"deviceIndex":2744,"channelIndex":8121}]}
     * * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} Request-Example:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":905,"data":{},"message":"hFh8sfu3"}
     */
    @PostMapping("/can/heartBeat")
    public ResponseResult heartBeat(@RequestBody MasterCanDto masterCanDto) {
        log.info("/terminal/api/v1/can/heartBeat masterCanDto: {}", masterCanDto);
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(canInfoService.heartBeat(masterCanDto));
        return responseResult;
    }
}
